﻿Imports Microsoft.Reporting.WinForms
Imports System.Windows.Forms.Integration
Imports System.IO

Partial Public Class ucReport
    Dim db As ATSDataClassesDataContext = New ATSDataClassesDataContext
    Dim host As WindowsFormsHost

    Private Sub ucReport_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Try

        
            Dim directoryList As DirectoryInfo = New DirectoryInfo(My.Settings.reportLocation)
            Dim directoryFiles As FileInfo() = directoryList.GetFiles("*.rdlc")
            Dim fileNameString As String = Nothing
            Dim fileDisplayString As String = Nothing
            tviAssetReport.Items.Clear()
            For Each item In directoryFiles
                Dim fileTreeviewItem As New TreeViewItem
                fileDisplayString = Replace(Replace(item.Name, "_", " "), ".rdlc", "")
                fileNameString = item.Name
                fileTreeviewItem.Tag = fileNameString
                fileTreeviewItem.Header = fileDisplayString
                Select Case fileNameString.Split("_")(0).ToUpper
                    Case "ASSETS"
                        tviAssetReport.Items.Add(fileTreeviewItem)
                    Case "ASSETSDEPRECIATION"
                        tviAssetDepreciationReport.Items.Add(fileTreeviewItem)
                    Case "ASSETSSUBSIDIARY"
                        tviAssetSubsidiaryReport.Items.Add(fileTreeviewItem)
                    Case "ASSETSEMPLOYEE"
                        tviAssetEmployeeReport.Items.Add(fileTreeviewItem)
                    Case "ASSETSLOCATION"
                        tviAssetLocationReport.Items.Add(fileTreeviewItem)
                    Case "ASSETSOTHER"
                        tviAssetOtherReport.Items.Add(fileTreeviewItem)
                End Select
                'item(0)
            Next

        Catch ex As Exception
            MessageBox.Show("Could not find reports in specified location. Please adjust report configurations." & ControlChars.Lf & ex.ToString, "ATS Report Error.", MessageBoxButton.OK, MessageBoxImage.Error)
        End Try



    End Sub

    Private Sub tvReportItems_SelectedItemChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of Object)) Handles tvReportItems.SelectedItemChanged
        Try

        
            If Not tvReportItems.SelectedItem.tag Is Nothing Then
                If File.Exists(My.Settings.reportLocation & "\" & tvReportItems.SelectedItem.tag) Then


                    gridReport.Children.Remove(host)
                    Dim companyDetails = From compDetails In db.CompanyDetails _
                                         Select compDetails.CompanyName

                    Dim inventoryDB = From t In db.MainAssetsTables _
                                      Group Join t0 In db.DisposedAssets On New With {t.Assetno} Equals New With {t0.Assetno} Into t0_join = Group _
                                      From t0 In t0_join.DefaultIfEmpty() _
                                      Group Join t1 In db.Categories On New With {t.CategoryCode} Equals New With {t1.CategoryCode} Into t1_join = Group _
                                      From t1 In t1_join.DefaultIfEmpty() _
                                      Group Join t2 In db.Locations On New With {t.LocationCode} Equals New With {t2.LocationCode} Into t2_join = Group _
                                      From t2 In t2_join.DefaultIfEmpty() _
                                      Group Join t3 In db.Departments On New With {.DeptCode = t.DeptCode} Equals New With {.DeptCode = t3.DepartmentCode} Into t3_join = Group _
                                      From t3 In t3_join.DefaultIfEmpty() _
                                      Where _
                                      t0.Assetno Is Nothing _
                                      Select _
                                      t.RFIDno, _
                                      t.Assetno, _
                                      t.DateAcquired, _
                                      t.AssetAmount, _
                                      t.CategoryCode, _
                                      t.LocationCode, _
                                      t.DeptCode, _
                                      t.Section, _
                                      t.AssignToEmployeeCode, _
                                      t.DescriptiveName, _
                                      t.Createdby, _
                                      t.DateCreated, _
                                      t.LastModified, _
                                      t.LastModifiedBy, _
                                      t.EstimatedValue, _
                                      t.ReplacementCost, _
                                      t.Condition, _
                                      t.Status, _
                                      t.SerialNumber, _
                                      t.InvoiceNumber, _
                                      t.BoughtFrom, _
                                      t.BaseCost, _
                                      t.VAT, _
                                      t.Manufacturer, _
                                      t.BrandName, _
                                      t.ModelNumber, _
                                      t.Remarks, _
                                      t.LastScanDate, _
                                      t.SalvageValue, _
                                      DepartmentId = CType(t3.DepartmentId, Int32?), _
                                      DepartmentName = t3.DepartmentName, _
                                      DepartmentCode = t3.DepartmentCode, _
                                      DepartmentDesc = t3.DepartmentDesc, _
                                      LocationId = CType(t2.LocationId, Int32?), _
                                      LocationName = t2.LocationName, _
                                      Column1 = t2.LocationCode, _
                                      LocationDesc = t2.LocationDesc, _
                                      CategoryId = CType(t1.CategoryId, Int32?), _
                                      CategoryName = t1.CategoryName, _
                                      Column2 = t1.CategoryCode, _
                                      CategoryDesc = t1.CategoryDesc, _
                                      DepreciationMethod = CType(t1.DepreciationMethod, Int32?), _
                                      Years = CType(t1.Years, Int32?), _
                                      AssetClass = t1.AssetClass




                    host = New WindowsFormsHost


                    Dim reportViewer As New Microsoft.Reporting.WinForms.ReportViewer
                    reportViewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local
                    reportViewer.LocalReport.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("ATS_MainAssetsTable", inventoryDB.OrderBy(Function(x) x.RFIDno).ThenBy(Function(p) p.Assetno).ToList))
                    reportViewer.LocalReport.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("ATS_CompanyDetail", companyDetails.First))
                    'reportViewer.LocalReport.ReportEmbeddedResource = "H:\Projects\NewATS\ATS\ATS\Reports\Asset Reports\ATS.AssetTagReport.rdlc" ' "ATS.AssetTagReport.rdlc"

                    reportViewer.LocalReport.ReportPath = My.Settings.reportLocation & "\" & tvReportItems.SelectedItem.tag ' "ATS.AssetTagReport.rdlc"
                    Dim reportParams(2) As ReportParameter
                    reportParams(0) = New ReportParameter("Report_Parameter_CompanyName", companyDetails.FirstOrDefault)
                    reportParams(1) = New ReportParameter("Report_Parameter_User", RibbonMain.v_username)
                    reportParams(2) = New ReportParameter("Report_Parameter_ShowAmount", My.Settings.reportHeaderShowAmounts) 'ribbonmain.v_username)
                    reportViewer.LocalReport.SetParameters(reportParams)
                    reportViewer.RefreshReport()

                    host.Child = reportViewer
                    gridReport.Children.Add(host)
                Else
                    MessageBox.Show("Report " & tvReportItems.Tag & " does not exist. Please check your report location. " & ControlChars.Lf & My.Settings.reportLocation)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
End Class

